package cn.david.org.service.impl;


import cn.david.basic.service.impl.BasicServiceImpl;
import cn.david.basic.util.MD5Utils;
import cn.david.basic.util.StrUtils;
import cn.david.org.domain.Employee;
import cn.david.org.mapper.EmployeeMapper;
import cn.david.org.service.IEmployeeService;
import cn.david.user.domian.Logininfo;
import cn.david.user.mapper.LogininfoMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

@Service
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class EmployeeServiceImpl extends BasicServiceImpl<Employee> implements IEmployeeService{
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private LogininfoMapper logininfoMapper;

    @Override
    public void add(Employee employee) {
        initEmployee(employee);
        Logininfo logininfo = new Logininfo();
        BeanUtils.copyProperties(employee, logininfo);
        logininfo.setType(0);
        logininfoMapper.save(logininfo);
        employee.setLogininfo_id(logininfo.getId());
        employeeMapper.save(employee);
    }

    private void initEmployee(Employee employee) {
        String salt = StrUtils.getComplexRandomString(32);
        String md5 = MD5Utils.encrypByMd5(employee.getPassword() + salt);
        employee.setSalt(salt);
        employee.setPassword(md5);

    }

    @Override
    @Transactional
    public void deleteByIds(Long[] ids) {
        employeeMapper.deleteByIds(ids);

    }


}
